//
//  HDVolumeSettingVC.m
//  HDSmartPenSDKDemo
//
//  Created by 牛的mini on 2016/12/26.
//  Copyright © 2016年 牛的mini. All rights reserved.
//

#import "HDVolumeSettingVC.h"

@interface HDVolumeSettingVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, assign) NSInteger volumeIndex;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@end

@implementation HDVolumeSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"音量设置";
    
    _volumeIndex = -1;
    _dataArray = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15"];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reloadStatusAction:)
                                                 name:kBeacon_SMPen_All_Info_Notification
                                               object:nil];
    _myTableView.delegate   = self;
    _myTableView.dataSource = self;
    _myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeNoti:) name:kBeacon_SMPen_Volume_Successful_Notification object:nil];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self requestSMPenInfoAction:nil];
}

- (IBAction)requestSMPenInfoAction:(id)sender {
    [[HDBeaconManager sharedBeacon] readAllInfoAction];
    [LCCoolHUD showLoading:@"正在获取" inView:self.view];
}

- (void)reloadStatusAction:(NSNotification *)noti
{
    NSArray *statusArray = noti.object;
    if (statusArray.count > 0)
    {
        NSString *str = statusArray[3];
        _volumeIndex = str.integerValue;
        [_myTableView reloadData];
    }
    [LCCoolHUD hideInView:self.view];
}

#pragma mark
#pragma mark -------- Tableview Delegate --------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ return _dataArray.count; }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"HDVolumeSettingVCCell";
    NSString *volumeStr = _dataArray[indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.textLabel.text = volumeStr;
    
    NSString *imgName = @"set_normal";
    if (indexPath.row == _volumeIndex)
        imgName = @"set_select";
    
    cell.imageView.image = [UIImage imageNamed:imgName];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row != _volumeIndex)
    {
        [[HDBeaconManager sharedBeacon] volumeIndexChange:indexPath.row];
    }
}

- (void)volumeNoti:(NSNotification *)noti
{
    NSString *str = noti.object;
    _volumeIndex = str.integerValue;
    [_myTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
